package com.mapfinal.server.service.wms;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.mapfinal.converter.Converter;
import com.mapfinal.converter.ConverterManager;
import com.mapfinal.server.MapfinalServer;

public class Wms {

	private String version = "1.1.1";
	private String request = "GetMap";
	private String format = "image/png";
	private boolean transparent = true;//TRANSPARENT
	private String styles;
	private String layers;
	private int width = 256;
	private int height = 256;
	private String srs = "EPSG:4326";
	private String bbox;
	private Envelope envelope = null;
	
	public Wms(String layers, String bbox) {
		this.layers = layers;
		this.bbox = bbox;
	}
	
	public Wms(Controller controller) {
		this.layers = controller.get("layers", controller.get("LAYERS"));
		this.bbox = controller.get("bbox", controller.get("BBOX"));
		this.srs = controller.get("srs", controller.get("SRS"));
		this.width = controller.getInt("width", controller.getInt("WIDTH", 256));
		this.height = controller.getInt("height", controller.getInt("HEIGHT", 256));
		this.styles = controller.get("styles", controller.get("STYLES"));
		this.request = controller.get("request", controller.get("REQUEST"));
	}
	
	public boolean isOk() {
		if(StrKit.isBlank(layers) || StrKit.isBlank(bbox)) {
			return false;
		}
		return true;
	}
	
	public Envelope toEnvelope() {
		if(envelope!=null) return envelope;
		if(StrKit.isBlank(bbox)) {
			return null;
		}
		String[] bboxs = bbox.split(",");
		if(bboxs.length < 4) return null;
		String xmin = bboxs[0];
		String xmax = bboxs[2];
		String ymin = bboxs[1];
		String ymax = bboxs[3];
		Double dxmin = Double.valueOf(xmin);
		Double dxmax = Double.valueOf(xmax);
		Double dymin = Double.valueOf(ymin);
		Double dymax = Double.valueOf(ymax);
		if(dxmin==null || dxmax==null || dymin==null || dymax==null)
			return null;
		if("EPSG:4326".equalsIgnoreCase(srs)) {
			envelope = new Envelope(dxmin, dxmax, dymin, dymax);
		} else {
			Converter converter = ConverterManager.use(srs, "EPSG:4326");
			Coordinate minCoord = converter.transform(dxmin, dymin, 0);
			Coordinate maxCoord = converter.transform(dxmax, dymax, 0);
			envelope = new Envelope(minCoord, maxCoord);
		}
		System.out.println("wms-bbox:" + envelope.toString());
		return envelope;
	}
	
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
	public String getRequest() {
		return request;
	}
	public void setRequest(String request) {
		this.request = request;
	}
	public String getImageFormat() {
		return MapfinalServer.imageFormat(format);
	}
	public String getFormat() {
		return format;
	}
	public void setFormat(String format) {
		this.format = format;
	}
	public boolean isTransparent() {
		return transparent;
	}
	public void setTransparent(boolean transparent) {
		this.transparent = transparent;
	}
	public String getStyles() {
		return styles;
	}
	public void setStyles(String styles) {
		this.styles = styles;
	}
	public String getLayers() {
		return layers;
	}
	public void setLayers(String layers) {
		this.layers = layers;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public String getSrs() {
		return srs;
	}
	public void setSrs(String srs) {
		this.srs = srs;
	}
	public String getBbox() {
		return bbox;
	}
	public void setBbox(String bbox) {
		this.bbox = bbox;
	}

	public Envelope getEnvelope() {
		return envelope;
	}

	public void setEnvelope(Envelope envelope) {
		this.envelope = envelope;
	}
	
}
